/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn;

import cn.webrx.config.SpringConfig;
import cn.webrx.mapper.StudentMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Project: mybatis2021 - Demo
 * <p>Powered by webrx On 2021-07-19 14:46:56
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 15
 */
@Slf4j
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class Demo {
    @Autowired
    private StudentMapper sm;

    @Autowired
    private SqlSession session;

    @Autowired
    private SqlSessionFactory sf;

    /**
     * mybatis 执行原生的jdbc SQL语句
     * @throws SQLException
     */
    @Test
    public void t9() throws SQLException {
        Connection conn = sf.openSession().getConnection();
        PreparedStatement pst = conn.prepareStatement("show tables");
        ResultSet rs = pst.executeQuery();
        while(rs.next()){
            System.out.println(rs.getString(1));
        }
        rs.close();
        pst.close();
        conn.close();
    }

    @Test
    public void t8() {
        Map<String, Object> map = new HashMap<String, Object>();
        //map.put("id", 33);
        map.put("ids", new int[]{1, 2, 3, 4, 8, 20});
        sm.mydel(map);
    }

    @Test
    public void t7() {
        //sm.myselect();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("age", 55);
        //map.put("id",3000);
        map.put("wname", "李%");
        sm.myupdate(map);
    }

    @Test
    public void t6() {
        Map<String, Object> map = new HashMap<String, Object>();
        sm.querys(map); //select * from t_student where 1=1

        map.put("age", 18);
        map.put("name", "李丽");
        map.remove("name");
        sm.querys(map);
    }

    @Test
    public void t5() {
        System.out.println(sm.showNames());

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", 4000);
        //map.put("name","李七");
        map.clear();
        System.out.println(sm.showNames(map));
    }


    @Test
    public void t4() {
        //mappers/StudentMapper.xml
        System.out.println(sm.ver());
        System.out.println(session.getMapper(StudentMapper.class).ver());

        //mappers/DbMapper.xml
        String v = session.selectOne("version");
        System.out.println(v);
    }


    @Test
    public void t3() {
        List<String> dbs = session.selectList("dbs");
        System.out.println(dbs);

        List<String> tbs = session.selectList("tbs");
        System.out.println(tbs);
    }

    @Test
    public void t2() {
        System.out.println(session);
        System.out.println(sf.openSession());

        String v = session.selectOne("version");
        System.out.println(v);

        String v2 = sf.openSession().selectOne("version");
        System.out.println(v2);
    }

    @Test
    public void t1() {
        //System.out.println(sm.ver());

        Map<String, Object> v = new HashMap<String, Object>();
        v.put("name", "李22");
        //v.put("id",3000);
        v.put("age", 11);
        sm.add(v);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", 3);
        map.put("age", 15);
        map.put("nid", 4000);
        map.put("name", "李四四");
        int n = sm.update(map);
        System.out.println(n);


        System.out.println(sm.queryById(4000));
    }
}
